using System.Collections.Generic;
using BCWAccountVip;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using I2.Loc;
using UnityEngine;

public class GameResultWindow : MonoBehaviour
{
	public Color colorBlue;

	public Color colorRed;

	public Color colorGold;

	public Color colorGray;

	public Color colorGreen;

	[SerializeField]
	private UILabel title;

	[SerializeField]
	private LeagueStats leagueStats;

	[SerializeField]
	private ZombieLeagueStats zombieLeagueStats;

	[SerializeField]
	private GameObject raceFinichedIcons;

	[SerializeField]
	private GameObject myTeamIcon;

	[SerializeField]
	private GameObject enemyTeamIcon;

	[SerializeField]
	private GameObject defead;

	[SerializeField]
	private GameObject draw;

	[SerializeField]
	private GameObject victory;

	public GameObject baseCaptured;

	[SerializeField]
	private MatchRewardManager matchRewardManager;

	[SerializeField]
	private UIWidget matchRewardWidget;

	[SerializeField]
	private UITexture matchRewardTexture;

	[SerializeField]
	private UIAnimation gameResultAnimation;

	[SerializeField]
	private PanelResultRewardUI panelResultReward;

	private bool showing;

	private TDMResult myTeamResult;

	private bool hidingMatchReward;

	public void Init()
	{
		leagueStats.Init();
		zombieLeagueStats.Init();
		matchRewardManager.Init();
	}

	public void ShowWithDelay()
	{
		Invoke("Show", 3f);
	}

	public void Show()
	{
		if (!showing)
		{
			showing = true;
			BCWStorage.Set("GetawayResult", 0, true);
			title.color = Color.white;
			myTeamIcon.SetActive(false);
			enemyTeamIcon.SetActive(false);
			raceFinichedIcons.SetActive(false);
			baseCaptured.SetActive(false);
			base.gameObject.SetActive(false);
			switch (GameController.instance.curTypeGame)
			{
			case TypeGame.race:
				SetRaceLabel(RaceController.instance.GetPlacePlayer(null));
				break;
			case TypeGame.TDM:
				myTeamResult = TDMController.MyTeamResult();
				SetTdmLabel(myTeamResult);
				break;
			case TypeGame.tanks:
				myTeamResult = TDMController.MyTeamResult();
				SetTdmLabel(myTeamResult);
				break;
			case TypeGame.CopsAndRobbers:
				myTeamResult = TDMController.MyTeamResult();
				SetCopsAndRobbersLabel(myTeamResult);
				break;
			case TypeGame.SkyBlock:
				myTeamResult = TDMController.MyTeamResult();
				SetTdmLabel(myTeamResult);
				break;
			case TypeGame.RocketLeague:
				myTeamResult = TDMController.MyTeamResult();
				SetTdmLabel(myTeamResult);
				break;
			case TypeGame.Zombe:
				myTeamResult = TDMController.MyTeamResult();
				SetZombieLabel(myTeamResult);
				break;
			case TypeGame.Pixelmon:
				myTeamResult = TDMController.MyTeamResult();
				SetTdmLabel(myTeamResult);
				break;
			case TypeGame.WalkingRobots:
				myTeamResult = TDMController.MyTeamResult();
				SetTdmLabel(myTeamResult);
				break;
			case TypeGame.TitanfallV2:
				SetDeathmatchLabel(0, 0);
				break;
			}
			title.gameObject.SetActive(true);
			if (GameController.instance.curTypeGame == TypeGame.TitanfallV2)
			{
				ShowReward();
			}
			else if (GameController.instance.curTypeGame == TypeGame.normal || GameController.instance.curTypeGame == TypeGame.OnlineSandbox || GameController.instance.curTypeGame == TypeGame.Offline)
			{
				Invoke("ShowReward", 1.5f);
			}
			else
			{
				Invoke("ShowReward", 3.5f);
			}
		}
	}

	private void OnDisable()
	{
		showing = false;
		matchRewardManager.gameObject.SetActive(false);
		title.gameObject.SetActive(false);
	}

	public void Hide()
	{
		title.gameObject.SetActive(false);
		base.gameObject.SetActive(false);
		HideResult();
	}

	private void SetDeathmatchLabel(int place, int count)
	{
		title.text = ScriptLocalization.Get("Game_Common/coop_results");
	}

	private void SetRaceLabel(int place)
	{
		raceFinichedIcons.SetActive(true);
		title.color = Color.white;
		if (place == 1)
		{
			title.color = colorGold;
		}
		title.text = ScriptLocalization.Get("Game_Common/race_place_" + place);
	}

	private void SetTdmLabel(TDMResult result)
	{
		switch (result)
		{
		case TDMResult.Winner:
			myTeamIcon.SetActive(true);
			victory.SetActive(true);
			defead.SetActive(false);
			draw.SetActive(false);
			title.color = colorBlue;
			title.text = ScriptLocalization.Get("Game_Common/coop_victory");
			break;
		case TDMResult.Loser:
			enemyTeamIcon.SetActive(true);
			victory.SetActive(false);
			defead.SetActive(true);
			draw.SetActive(false);
			title.color = colorRed;
			title.text = ScriptLocalization.Get("Game_Common/coop_defeat");
			break;
		case TDMResult.Draw:
			myTeamIcon.SetActive(true);
			enemyTeamIcon.SetActive(true);
			victory.SetActive(false);
			defead.SetActive(false);
			draw.SetActive(true);
			title.color = colorGray;
			title.text = ScriptLocalization.Get("Game_Common/coop_draw");
			break;
		}
		bool flag = false;
		baseCaptured.SetActive(BCWFlagManager.instance != null && BCWFlagManager.instance.winerTeam != PunTeams.Team.none);
	}

	public void HideResult()
	{
		victory.SetActive(false);
		defead.SetActive(false);
		draw.SetActive(false);
	}

	private void SetCopsAndRobbersLabel(TDMResult result)
	{
		switch (result)
		{
		case TDMResult.Winner:
			title.color = colorBlue;
			title.text = ScriptLocalization.Get("Game_Common/coop_victory");
			myTeamIcon.SetActive(true);
			victory.SetActive(true);
			defead.SetActive(false);
			draw.SetActive(false);
			break;
		case TDMResult.Loser:
			title.color = colorRed;
			title.text = ScriptLocalization.Get("Game_Common/coop_defeat");
			enemyTeamIcon.SetActive(true);
			title.color = colorRed;
			victory.SetActive(false);
			defead.SetActive(true);
			draw.SetActive(false);
			break;
		}
		if (PhotonNetwork.isMasterClient)
		{
			bool flag = result == TDMResult.Winner && TDMController.myTeam == PunTeams.Team.blue;
			flag |= result == TDMResult.Loser && TDMController.myTeam == PunTeams.Team.red;
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Boiler " + (controllerConnectPhoton.thisScript.playerBoiler + 1), (!flag) ? "Cops wins" : "Robbers wins");
			FlurryWrapper.LogEvent(FlurryWrapper.EV_COPS_AND_ROBBERS_STATS, dictionary);
		}
	}

	private void SetZombieLabel(TDMResult result)
	{
		switch (result)
		{
		case TDMResult.Winner:
			if (TDMController.myTeam == PunTeams.Team.blue)
			{
				title.color = colorBlue;
				title.text = ScriptLocalization.Get("Game_Common/sm_human_win");
			}
			else
			{
				title.color = colorGreen;
				title.text = ScriptLocalization.Get("Game_Common/sm_zombie_win");
			}
			victory.SetActive(true);
			defead.SetActive(false);
			draw.SetActive(false);
			break;
		case TDMResult.Loser:
			if (TDMController.myTeam == PunTeams.Team.red)
			{
				title.color = colorBlue;
				title.text = ScriptLocalization.Get("Game_Common/sm_human_win");
			}
			else
			{
				title.color = colorGreen;
				title.text = ScriptLocalization.Get("Game_Common/sm_zombie_win");
			}
			victory.SetActive(false);
			defead.SetActive(true);
			draw.SetActive(false);
			break;
		case TDMResult.Draw:
			title.color = Color.white;
			title.text = ScriptLocalization.Get("Game_Common/sm_zombie_teamleave");
			victory.SetActive(false);
			defead.SetActive(true);
			draw.SetActive(false);
			break;
		}
		if (PhotonNetwork.isMasterClient)
		{
			bool flag = result == TDMResult.Winner && TDMController.myTeam == PunTeams.Team.blue;
			flag |= result == TDMResult.Loser && TDMController.myTeam == PunTeams.Team.red;
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Match_result", (!flag) ? "Zombie wins" : "Human wins");
			dictionary.Add("Boiler " + (controllerConnectPhoton.thisScript.playerBoiler + 1), (!flag) ? "Zombie wins" : "Human wins");
			dictionary.Add("Players_count", PhotonNetwork.playerList.Length.ToString());
			FlurryWrapper.LogEvent(FlurryWrapper.EV_ZOMBIE_MODE_STATS, dictionary);
		}
	}

	private void ShowReward()
	{
		base.gameObject.SetActive(true);
		gameResultAnimation.AnimAlpha(0f, 1f, 1f, false);
		int num = 0;
		InfoMatchReward item = default(InfoMatchReward);
		item.id = string.Empty;
		int num2 = ((!BonusAwardManager.instance.IsSetBonus(GameController.instance.curTypeGame) || !myTeamResult.Equals(TDMResult.Winner)) ? 1 : 2);
		bool isActiveVip = ManagerVip.GetInstance().IsActiveVip();
		switch (GameController.instance.curTypeGame)
		{
		case TypeGame.race:
			num = CalcResultStars(RaceController.instance.GetPlacePlayer(null), RaceController.instance.GetPlayerCount(), TDMResult.Winner);
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, RaceController.instance.GetPlacePlayer(null), 0, num2, isActiveVip, TDMResult.Winner);
			break;
		case TypeGame.TDM:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, myTeamResult);
			break;
		}
		case TypeGame.SkyBlock:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, myTeamResult);
			break;
		}
		case TypeGame.CopsAndRobbers:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, myTeamResult);
			break;
		}
		case TypeGame.RocketLeague:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, myTeamResult);
			break;
		}
		case TypeGame.tanks:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, myTeamResult);
			break;
		}
		case TypeGame.Zombe:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStarsForZombie(TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, 0, num2, isActiveVip, myTeamResult);
			if (myTeamResult.Equals(TDMResult.Winner))
			{
				GameInfoWindow.instance.ShowShopAndMoney(false, true);
			}
			else
			{
				GameInfoWindow.instance.ShowShopAndMoney(true, false);
			}
			break;
		}
		case TypeGame.Pixelmon:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, myTeamResult);
			break;
		}
		case TypeGame.WalkingRobots:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, myTeamResult);
			break;
		}
		case TypeGame.TitanfallV2:
		{
			List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
			num = CalcResultStars(PVPManager.instance.myPlace, GameController.instance.listPlayers.Count, TDMController.MyTeamResult());
			item = matchRewardManager.GetReward(GameController.instance.curTypeGame, PVPManager.instance.myPlace, PVPManager.instance.countPlayersInMyTeam, num2, isActiveVip, TDMResult.Winner);
			break;
		}
		}
		CupsManager.Cups += num;
		panelResultReward.BonusFactorAward = num2;
		panelResultReward.IsActiveVip = isActiveVip;
		if (!string.IsNullOrEmpty(item.id))
		{
			panelResultReward.Rewards.Add(item);
		}
		InfoMatchReward infoMatchReward = default(InfoMatchReward);
		if (GameController.instance.curTypeGame != TypeGame.Zombe)
		{
			leagueStats.gameObject.SetActive(true);
			zombieLeagueStats.gameObject.SetActive(false);
			infoMatchReward = leagueStats.AnimateCups(num2);
		}
		else
		{
			leagueStats.gameObject.SetActive(false);
			zombieLeagueStats.gameObject.SetActive(true);
			infoMatchReward = zombieLeagueStats.AnimateCups(num2);
		}
		if (!string.IsNullOrEmpty(infoMatchReward.id) && infoMatchReward.count > 0)
		{
			panelResultReward.Rewards.Add(infoMatchReward);
		}
		if (panelResultReward.Rewards.Count > 0)
		{
			panelResultReward.StartPosition = base.gameObject.transform.position;
			panelResultReward.Show();
			matchRewardWidget.alpha = 1f;
			matchRewardManager.gameObject.SetActive(true);
		}
	}

	public void HideMatchReward()
	{
		if (hidingMatchReward)
		{
			return;
		}
		panelResultReward.GiveReward();
		hidingMatchReward = true;
		HOTween.To(matchRewardWidget, 0.75f, new TweenParms().Prop("alpha", 0).Ease(EaseType.EaseInExpo).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			if (GameController.instance.curTypeGame == TypeGame.Zombe)
			{
				GameInfoWindow.instance.ShowShopAndMoney(true, false);
			}
			GameInfoWindow.instance.ShowShopAndMoney(null, false);
			hidingMatchReward = false;
		}));
	}

	public int CalcResultStars(int place, int playersCount, TDMResult myTeamResult)
	{
		int num = 10;
		int[] array = new int[10];
		int[] array2 = new int[10];
		int[] array3 = new int[10];
		float num2 = 1f;
		int num3 = 0;
		switch (GameController.instance.curTypeGame)
		{
		case TypeGame.race:
			num = 5;
			array = new int[7] { 20, 15, 10, 5, -5, 0, 0 };
			break;
		case TypeGame.TDM:
			num = 7;
			array = new int[7] { 45, 30, 15, 10, 5, 0, 0 };
			array2 = new int[7] { 15, 10, 5, -5, -10, -15, -20 };
			array3 = new int[7] { 15, 10, 5, 0, 0, 0, 0 };
			break;
		case TypeGame.SkyBlock:
			num = 5;
			array = new int[7] { 45, 30, 15, 5, 0, 0, 0 };
			array2 = new int[7] { 15, 10, 5, -5, 10, 0, 0 };
			array3 = new int[7] { 15, 10, 5, 0, 0, 0, 0 };
			break;
		case TypeGame.RocketLeague:
			num = 3;
			array = new int[7] { 30, 10, 0, 0, 0, 0, 0 };
			array2 = new int[7] { 0, -5, -10, 0, 0, 0, 0 };
			array3 = new int[7];
			break;
		case TypeGame.tanks:
			num = 5;
			array = new int[7] { 45, 30, 15, 10, 5, 0, 0 };
			array2 = new int[7] { 15, 10, 5, -5, -10, -15, -20 };
			array3 = new int[7] { 15, 10, 5, 0, 0, 0, 0 };
			break;
		case TypeGame.Pixelmon:
			num = 7;
			array = new int[7] { 45, 30, 15, 10, 5, 0, 0 };
			array2 = new int[7] { 15, 10, 5, -5, -10, -15, -20 };
			array3 = new int[7] { 15, 10, 5, 0, 0, 0, 0 };
			break;
		case TypeGame.WalkingRobots:
			num = 7;
			array = new int[7] { 45, 30, 15, 10, 5, 0, 0 };
			array2 = new int[7] { 15, 10, 5, -5, -10, -15, -20 };
			array3 = new int[7] { 15, 10, 5, 0, 0, 0, 0 };
			break;
		case TypeGame.TitanfallV2:
			num = 10;
			array3 = new int[10] { 45, 30, 15, 10, 5, 0, -5, -10, -15, -20 };
			break;
		case TypeGame.CopsAndRobbers:
			num = 5;
			array = new int[7] { 45, 30, 15, 5, 0, 0, 0 };
			array2 = new int[7] { 15, 10, 5, -5, -10, -15, -20 };
			array3 = new int[7] { 15, 10, 5, 0, 0, 0, 0 };
			break;
		}
		switch (myTeamResult)
		{
		case TDMResult.Winner:
			num3 = array[place - 1];
			break;
		case TDMResult.Loser:
			num3 = array2[place - 1];
			break;
		case TDMResult.Draw:
			num3 = array3[place - 1];
			break;
		}
		if (playersCount < num)
		{
			num2 = (float)playersCount / (float)num;
		}
		return (int)((float)num3 * num2);
	}

	public int CalcResultStarsForZombie(TDMResult myTeamResult)
	{
		int num = 15;
		int num2 = 15;
		int num3 = -15;
		int num4 = -15;
		int num5 = -15;
		int num6 = 1;
		int num7 = 4;
		int num8 = 6;
		int num9 = 0;
		int num10 = 0;
		num10 = num6 * ZombieModePlayerStatistics.killsAsHuman;
		zombieLeagueStats.SetZombieKillsLabel(ZombieModePlayerStatistics.killsAsHuman, num10);
		num9 += num10;
		num10 = num7 * ZombieModePlayerStatistics.killsAsZombie;
		zombieLeagueStats.SetHumanKillsLabel(ZombieModePlayerStatistics.killsAsZombie, num10);
		num9 += num10;
		num10 = Mathf.FloorToInt((float)num8 * ZombieModePlayerStatistics.surviveTimeInSeconds / 60f);
		if (num10 > 24)
		{
			num10 = 24;
		}
		zombieLeagueStats.SetSurviveTimeLabel(ZombieModePlayerStatistics.surviveTimeInSeconds, num10);
		num9 += num10;
		num10 = (ZombieModePlayerStatistics.wasInfected ? num5 : 0);
		num9 += num10;
		switch (myTeamResult)
		{
		case TDMResult.Winner:
			num10 = ((ZombieModePlayerStatistics.team != PunTeams.Team.red) ? num : num2);
			zombieLeagueStats.SetTeamWinsLabel(true, num10);
			num9 += num10;
			break;
		case TDMResult.Loser:
			num10 = ((ZombieModePlayerStatistics.team != PunTeams.Team.red) ? num3 : num4);
			zombieLeagueStats.SetTeamWinsLabel(false, num10);
			num9 += num10;
			break;
		case TDMResult.Draw:
			num10 = ((ZombieModePlayerStatistics.team != PunTeams.Team.red) ? 0 : 0);
			zombieLeagueStats.SetTeamWinsLabel(false, num10, true);
			num9 += num10;
			break;
		}
		return num9;
	}

	public void GetawayResult()
	{
		bool flag = true;
		int getawayResult = 0;
		if (!ReturnGetawayResult(ref getawayResult))
		{
			BCWStorage.Set("GetawayResult", 0, true);
		}
		else
		{
			BCWStorage.Set("GetawayResult", getawayResult, true);
		}
	}

	public bool ReturnGetawayResult(ref int getawayResult)
	{
		bool result = true;
		getawayResult = 0;
		int num = ((TDMController.instance.CountTeam(TDMController.myTeam) == 0) ? 1 : TDMController.instance.CountTeam(TDMController.myTeam));
		switch (GameController.instance.curTypeGame)
		{
		case TypeGame.race:
			if (!RaceController.instance.enableMove || RaceController.instance.gameIsEnded)
			{
				result = false;
			}
			if (GameController.instance.listPlayers.Count > 1)
			{
				getawayResult = -30;
			}
			break;
		case TypeGame.TDM:
			if (PlayerBehavior.MyPlayer == null || !PlayerBehavior.MyPlayer.startPVP)
			{
				result = false;
			}
			if (TDMController.instance.CountTeam(TDMController.enemyTeam) > 0 && TDMController.instance.CountTeam(TDMController.enemyTeam) / num < 2)
			{
				getawayResult = -30;
			}
			break;
		case TypeGame.SkyBlock:
			if (PlayerBehavior.MyPlayer == null || !PlayerBehavior.MyPlayer.startPVP)
			{
				result = false;
			}
			if (TDMController.instance.CountTeam(TDMController.enemyTeam) > 0 && TDMController.instance.CountTeam(TDMController.enemyTeam) / num < 2)
			{
				getawayResult = -30;
			}
			break;
		case TypeGame.RocketLeague:
			if (PlayerBehavior.MyPlayer == null || !PlayerBehavior.MyPlayer.startPVP)
			{
				result = false;
			}
			if (TDMController.instance.CountTeam(TDMController.enemyTeam) > 0)
			{
				getawayResult = -30;
			}
			break;
		case TypeGame.CopsAndRobbers:
			if (PlayerBehavior.MyPlayer == null || !PlayerBehavior.MyPlayer.startPVP || !CopsAndRobbersController.instance.gameIsStarted)
			{
				result = false;
			}
			getawayResult = -30;
			break;
		case TypeGame.Zombe:
			if (PlayerBehavior.MyPlayer == null || !PlayerBehavior.MyPlayer.startPVP)
			{
				result = false;
			}
			getawayResult = -30;
			break;
		case TypeGame.tanks:
			if (PlayerBehavior.MyPlayer == null || !PlayerBehavior.MyPlayer.startPVP)
			{
				result = false;
			}
			if (TDMController.instance.CountTeam(TDMController.enemyTeam) > 0 && TDMController.instance.CountTeam(TDMController.enemyTeam) / num < 2)
			{
				getawayResult = -30;
			}
			break;
		case TypeGame.WalkingRobots:
			if (PlayerBehavior.MyPlayer == null || !PlayerBehavior.MyPlayer.startPVP)
			{
				result = false;
			}
			if (TDMController.instance.CountTeam(TDMController.enemyTeam) > 0 && TDMController.instance.CountTeam(TDMController.enemyTeam) / num < 2)
			{
				getawayResult = -30;
			}
			break;
		}
		if (getawayResult >= 0 || controllerConnectGame.reconnectRoom)
		{
			result = false;
		}
		return result;
	}
}
